package com.ybkj.daijia.serverUtils;

import com.ybkj.daijia.server.zhuan.SmallLocation;
import java.text.DecimalFormat;

/**
 * @author zhyu
 * @date 2019-09-26 16:16:07
 */
public class GPSTransformUtils {

    public static final double x_pi = Math.PI * 3000.0 / 180.0;

    /**
     * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param bd_lat * @param
     * bd_lon * @return
     */
    public static double[] bd09_To_Gcj02(double lat, double lon) {
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta);
        double tempLat = z * Math.sin(theta);
        double[] gps = {tempLat, tempLon};
        return gps;
    }

    /**
     * * 火星坐标系 (GCJ-02)/高德在国内就是 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param bd_lat *
     *  @return
     */
    public static void bd09_To_Gcj02(SmallLocation location) {
        if (location == null) {
            return;
        }
        double x = location.getLongitude() - 0.0065, y = location.getLatitude() - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        DecimalFormat myDF = new DecimalFormat("0.000000");
        double tempLon = z * Math.cos(theta);
        double tempLat = z * Math.sin(theta);
        location.setLatitude(Double.valueOf(myDF.format(tempLat)));
        location.setLongitude(Double.valueOf(myDF.format(tempLon)));
    }
}
